﻿using Godot.core;

namespace Godot.core.os
{
    using ThreadCreateCallback = System.Action<object>;

    public abstract class Thread
    {
        public enum Priority
        {
            PRIORITY_LOW,
            PRIORITY_NORMAL,
            PRIORITY_HIGH
        };

        public class Settings
        {
            public Priority priority;
            
            public Settings() 
            { 
                priority = Priority.PRIORITY_NORMAL; 
            }
        };

        internal static ulong _main_thread_id;

        protected static System.Func<ThreadCreateCallback, object, Settings, Thread> create_func;

        protected static System.Func<ulong> get_thread_ID_func;

        protected static System.Action<Thread> wait_to_finish_func;

        protected Thread() {}

        public abstract ulong ID { get; }

        public static ulong main_ID
        {
            get { return _main_thread_id; }
        }

        public static ulong caller_ID
        {
            get
            {
                if (get_thread_ID_func != null)
                    return get_thread_ID_func();
                return 0;
            }
        }

        public static void wait_to_finish(Thread p_thread)
        {
            if (wait_to_finish_func != null)
                wait_to_finish(p_thread);
        }

        public static Thread create(ThreadCreateCallback p_callback, object p_user, Settings p_settings = null)
        {
            if (create_func != null)
                return create_func(p_callback, p_user, p_settings);
            return null;
        }
    }
}
